package com.four.user.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.four.user.pojo.User;
import com.four.user.service.UserService;
import com.four.user.service.impl.UserServiceImpl;
import com.google.gson.Gson;
//设置网络访问地址
@WebServlet("/selectUser.do")
public class SelectUserAllServlet extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		resp.setCharacterEncoding("UTF-8");
		UserService service = new UserServiceImpl();
		List<User> list = service.selectAll();
		//设置返回数据的格式为JSON（特殊格式的字符串）
		resp.setContentType("application/json");
		PrintWriter pw = resp.getWriter();
		Gson gson = new Gson();
		String result = gson.toJson(list);
		System.out.println(result);
		pw.append(result);
		pw.flush();
		pw.close();
}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		this.doGet(req, resp);
}

}
